/**
 * Copyright (c) 2018-2019
 * All Rights Reserved by Thunder Software Technology Co., Ltd and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with THUNDERSOFT in writing by applicable law.
 */

package com.kll.carolinehmibiz.base.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * SpUtils.
 *
 * @author: jiangwei
 * @Date: 19-2-28 PM2:43
 */
public class SpUtils {
    private SharedPreferences mPreferences = null;
    private SharedPreferences.Editor mEditor = null;
    private static SpUtils sInstance;

    private static final String TYPE_STRING = "String";
    private static final String TYPE_INTEGER = "Integer";
    private static final String TYPE_BOOLEAN = "Boolean";
    private static final String TYPE_FLOAT = "Float";
    private static final String TYPE_LONG = "Long";

    private SpUtils() {
    }

    /**
     * getInstance.
     *
     * @return SpUtils
     */
    public static synchronized SpUtils getInstance() {
        if (sInstance == null) {
            sInstance = new SpUtils();
        }
        return sInstance;
    }

    private SharedPreferences getPreferences(Context context) {
        if (mPreferences == null) {
            mPreferences =
                    PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        }
        return mPreferences;
    }

    public String getValueByString(Context context, String key){
        return getInstance().getPreferences(context).getString(key, null);
    }

    public int getValueByInt(Context context,String key){
        return getInstance().getPreferences(context).getInt(key, -1);
    }

    public float getValueByFloat(Context context,String key) {
        return getInstance().getPreferences(context).getFloat(key, -1);
    }

    public boolean getValueByBoolean(Context context,String key){
        return getInstance().getPreferences(context).getBoolean(key, false);
    }

    public boolean getValueByBoolean(Context context,String key, boolean defValue){
        return getInstance().getPreferences(context).getBoolean(key, defValue);
    }

    public long getValueByLong(Context context,String key){
        return getInstance().getPreferences(context).getLong(key, -1);
    }

    public void saveValueToSharePerference(Context context,String key, boolean value){
        SharedPreferences.Editor editor = getInstance().getPreferences(context).edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public void saveValueToSharePerference(Context context,String key, int value){
        SharedPreferences.Editor editor = getInstance().getPreferences(context).edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public void saveValueToSharePerference(Context context,String key, String value){
        SharedPreferences.Editor editor = getInstance().getPreferences(context).edit();
        editor.putString(key, value);
        editor.commit();
    }

    public void saveValueToSharePerference(Context context,String key, long value){
        SharedPreferences.Editor editor = getInstance().getPreferences(context).edit();
        editor.putLong(key, value);
        editor.commit();
    }

    public void removeValueToSharePerference(Context context,String key){
        SharedPreferences.Editor editor = getInstance().getPreferences(context).edit();
        editor.remove(key);
        editor.commit();
    }

    public void clear(Context context){
        SharedPreferences.Editor editor = getInstance().getPreferences(context).edit();
        editor.clear();
        editor.commit();
    }
}